/**
 * 测试用例编组添加接口测试用例
 *
 * Created by liuxuechao on 2022/10/19.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmark = require('./../../../benchmark/generate-tests/self_designed');

module.exports = describe('/POST /functions/:functionId/testcase-groups.json', function () {
  const administrator = new Administrator();
  let project = {};
  const newUserIds = [];
  function url(functionId) {
    return `/functions/${functionId}/testcase-groups.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });

  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'post');
  common.should.be.tester(uri, url, 'post');

  // 正常发起请求
  it(`it should add testcase group for ${benchmark.name} by data`, function (done) {
    let projectVersion;
    let func;
    project.createRandomVersion()
      .then((res) => {
        projectVersion = res;
        return project.creator.login();
      })
      .then(() => projectVersion.getAllFunctions(benchmark))
      .then((res) => {
        [func] = res;
        const { accessToken } = projectVersion.project.creator.token;
        const form = {
          parentId: null,
          description: '13579',
        };
        chai.request(uri)
          .post(url(func.functionId))
          .set('Authorization', `Bearer ${accessToken}`)
          .send(form)
          .end((err, res) => {
            res.should.have.status(200);
            done();
          });
      })
      .catch(done);
  });
});
